package com.hy.mydesktop.client.mvc.core.builder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.hy.mydesktop.client.component.meta.GridColumnConfigModel;
import com.hy.mydesktop.client.component.meta.GridModelEnum;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.client.component.type.ComponentControllerEnum;
import com.hy.mydesktop.client.component.type.ComponentTypeEnum;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtAppEventType;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentControllerMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.ItemOfAssociateToWindow;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.MetaDataConstants;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceParameter;

public class GridBuilderTemplate {

	public static GxtComponentControllerMetaNodeModel createGridBuilderTemplate(
			Map<String, Object> map1, Map<String, Object> map2) {
		Map<String , Object> gridController=new HashMap<String, Object>();
		gridController.put("component_controller_model_type", ComponentControllerEnum.GRIDSETMODULES_CONTROLLER_MODEL);
		gridController.put("component_controller_model_type_index", ComponentControllerEnum.GRIDSETMODULES_CONTROLLER_MODEL.ordinal());
		
		String[] appEventIds={"0301","0401"};

		
		GxtComponentControllerMetaNodeModel node6=new GxtComponentControllerMetaNodeModel(gridController);
		node6 = ControllerAndComponentMetaNodeModelCreationTemplate.buildCommonAttributes(node6,map1);
		if (map1.get(MetaDataConstants.COMPONENT_CONTROLLER_METAMODEL_ID)!=null) {	
			String controllerMetaModelId=(String)map1.get(MetaDataConstants.COMPONENT_CONTROLLER_METAMODEL_ID);
			String id="gridSetModuleController_"+controllerMetaModelId;
			node6.setId(id);
			node6.setControllerMetaNodeModelId(id);
		}else {
			throw new RuntimeException("xxxxxxxxxxxxx");
		}
		
		node6.setRegisterEventTypes(node6.addGxtAppEventType(appEventIds));
		

		
		map2.put(MetaDataConstants.COMPONENT_CONTROLLER_METAMODEL_ID, node6.getControllerMetaNodeModelId());
		GxtComponentMetaNodeModel gxtComponentMetaNodeModel =getGridSetModuleGxtComponentMetaNodeModel2(map2);
		node6.setGxtComponentMetaNodeModel(gxtComponentMetaNodeModel);
		
		return node6;
		
	}

	private static GxtComponentMetaNodeModel getGridSetModuleGxtComponentMetaNodeModel2(
			Map<String, Object> map2) {
		
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		//map =configureCommonAttributesOfGridMap2(map2);
		
		
		map.put("component_model_type", ComponentTypeEnum.GRIDSETMODULES_MODEL);
		map.put("component_model_type_index", ComponentTypeEnum.GRIDSETMODULES_MODEL.ordinal());
		map.put("autowidth", true);
		map.put("autoheight", true);
		map.put("frame", true);
		map.put("headervisible", false);		
		//此属性，可以不写（setGridColumnConfigModels方法中，已经设置过了）
		map.put(ComponentMetaDataConstants.GRIDMODULE_TYPE, ComponentTypeEnum.GRIDMODULE_MODEL.toString());
//		map.put("version",1);//可编辑表格
		String gridModuleTypeVersion=(String)map2.get(ComponentMetaDataConstants.GRIDMODULE_TYPE_VERSION);

		if (gridModuleTypeVersion==null) {
			throw new RuntimeException("sssssssssssssssssssssssssssssssss");
			
		}else {
			//map.put(ComponentMetaDataConstants.GRIDMODULE_TYPE_VERSION,  ComponentMetaDataConstants.EDITORPAGING_GRIDMODULETYPE) ;
			map.put(ComponentMetaDataConstants.GRIDMODULE_TYPE_VERSION, 
					map2.get(ComponentMetaDataConstants.GRIDMODULE_TYPE_VERSION)) ;
		}
		if (map2.get(MetaDataConstants.FORM_ID)==null) {
			throw new RuntimeException("sssssssssssssssssssssssssssssssss");
			
		}else {
			map.put(MetaDataConstants.FORM_ID, map2.get(MetaDataConstants.FORM_ID)) ;
		}
		if (map2.get(MetaDataConstants.COMPONENT_CONTROLLER_METAMODEL_ID)==null) {
			throw new RuntimeException("sssssssssssssssssssssssssssssssss");
		}else {
			map.put(MetaDataConstants.COMPONENT_CONTROLLER_METAMODEL_ID, map2.get(MetaDataConstants.COMPONENT_CONTROLLER_METAMODEL_ID)) ;
		}
		map.put("height", 370);
		//map3.put("width", 500);
		map.put("headervisible",false );
		map.put("rownumber", true);																				
		//map2.put("columnconfigload", columnConfigs);
		//map2.put(ComponentMetaDataConstants.GRID_COLUMNCONFIG_MODELS,columnConfigs);								
		GxtComponentMetaNodeModel node2 = new GxtComponentMetaNodeModel(map);				
		
		if (map2.get(ComponentMetaDataConstants.GRID_COLUMNCONFIG_MODELS)!=null) {
			List<GridColumnConfigModel> columnConfigs=(List<GridColumnConfigModel>) map2.get(ComponentMetaDataConstants.GRID_COLUMNCONFIG_MODELS);
			node2.setGridColumnConfigModels(columnConfigs);

			node2=configureItems(node2,map2);
		}else {
			throw new RuntimeException("sssssssssssssssssssssssssssssssss");

		}
		
		//HandlerData.data ( map2 , "positionTypeService" , "getAllPositonType" , "addPositionType" , "modifyPositionType" , "loadEditorableGrid"  );				
////////////////////////////////////////////////////////////////////////////////////////////////////
		/*List<ItemOfAssociateToWindow> childrenItems=new LinkedList<ItemOfAssociateToWindow>();
				GxtToSeamServiceParameter g1=new GxtToSeamServiceParameter("positionTypeService" , "getAllPositonType" );
				g1.setMethodArgumentsType(null);
				ItemOfAssociateToWindow itemOfAssociateToWindow = new ItemOfAssociateToWindow();
				itemOfAssociateToWindow.setGxtToSeamServiceParameter(g1);
				EventStructureMetaModel eventStructureMetaModel = new EventStructureMetaModel();
				eventStructureMetaModel.setRootControllerId("1_1_windowController_PositonType");
				eventStructureMetaModel.setControllerId("4_1_gridController_PositonType");
				itemOfAssociateToWindow.setEventStructureMetaModel(eventStructureMetaModel);
				itemOfAssociateToWindow.setItemType(ComponentMetaDataConstants.MVC_ITEM_TYPE);
				itemOfAssociateToWindow.setRegisterEventType(new GxtAppEventType("0401"));
			
				//childrenItems.add(ItemOfAssociateToWindow.addGxtToSeamServiceParameterToItem("gxttoseamserviceparametermodel","positionTypeService" , "getAllPositonType" ));
				//childrenItems.add(ItemOfAssociateToWindow.addGxtToSeamServiceParameterToItem("gxttoseamserviceparametermodelforadd","positionTypeService" , "addPositionType" ));
				//childrenItems.add(ItemOfAssociateToWindow.addGxtToSeamServiceParameterToItem("gxttoseamserviceparametermodelforupdate","positionTypeService" , "modifyPositionType" ));
				//childrenItems.add(ItemOfAssociateToWindow.addGxtToSeamServiceParameterToItem("gxttoseamserviceparametermodelformerge","positionTypeService" , "loadEditorableGrid" ));
			childrenItems.add(itemOfAssociateToWindow);
		node2.set (ComponentMetaDataConstants.CHILDREN_ITEMS,  childrenItems) ;
		//此属性，可以不写（setGridColumnConfigModels方法中，已经设置过了）
		node2.setChildrenItemsType("data");
		
		if(map2.get(MetaDataConstants.APP_EVENT_TYPE)!=null&&(map2.get(MetaDataConstants.APP_EVENT_TYPE) instanceof String)){
			String gxtAppEventType= map2.get(MetaDataConstants.APP_EVENT_TYPE).toString();
			itemOfAssociateToWindow.setRegisterEventType(new GxtAppEventType(gxtAppEventType));

		}else {
			throw new RuntimeException("sssssssssssssssssssssssssssssssss");
		}*/
///////////////////////////////////////////////////////////////////////////////////////////////
		
//////////////////////////////设置表格的表格字段/////////////////////////////////////////////////		
		/*List<GridColumnConfigModel> columnConfigs = new LinkedList<GridColumnConfigModel>();		
			columnConfigs.add(new GridColumnConfigModel("TITLE","名称",200));
			columnConfigs.add(new GridColumnConfigModel("DESCRIPTION","描述",300));
		//node2.set(ComponentMetaDataConstants.GRID_COLUMNCONFIG_MODELS,columnConfigs);
		node2.setGridColumnConfigModels(columnConfigs);*/
		

/////////////////////////////////////////////////////////////////////////////////////////////////		

	//	node2 =ControllerAndComponentMetaNodeModelCreationTemplate.removeUnusedProperties(node2);

		return node2;
	}

	public static ItemOfAssociateToWindow createItemForEditorPagingGrid(
			String controllerMetaModelId,String formId) {
//		List<ItemOfAssociateToWindow> childrenItems=new LinkedList<ItemOfAssociateToWindow>();
//		GxtToSeamServiceParameter g1=new GxtToSeamServiceParameter("positionTypeService" , "getAllPositonType" );
//		GxtToSeamServiceParameter g1=new GxtToSeamServiceParameter("positionTypeService" , "getAllPositonType" );
		GxtToSeamServiceParameter g1=new GxtToSeamServiceParameter(formId , "getPageOfPersistentEntities" );
		g1.setMethodArgumentsType(null);
		ItemOfAssociateToWindow itemOfAssociateToWindow = new ItemOfAssociateToWindow();
		itemOfAssociateToWindow.setGxtToSeamServiceParameter(g1);
		EventStructureMetaModel eventStructureMetaModel = new EventStructureMetaModel();
		eventStructureMetaModel.setRootControllerId("windowController_"+controllerMetaModelId);
		eventStructureMetaModel.setControllerId("gridSetModuleController_"+controllerMetaModelId);
		itemOfAssociateToWindow.setEventStructureMetaModel(eventStructureMetaModel);
		itemOfAssociateToWindow.setItemType(ComponentMetaDataConstants.MVC_ITEM_TYPE);
		itemOfAssociateToWindow.setRegisterEventType(new GxtAppEventType("0401"));

		return itemOfAssociateToWindow;
		
	}

	private static GxtComponentMetaNodeModel configureItems(GxtComponentMetaNodeModel node2, Map<String, Object> map2) {
		if (map2.get(ComponentMetaDataConstants.CHILDREN_ITEMS)!=null) {			
				if (node2.getChildrenItemsType().equals("data")) {
					
						List<ItemOfAssociateToWindow> childrenItems=(List<ItemOfAssociateToWindow>) map2.get(ComponentMetaDataConstants.CHILDREN_ITEMS);
						if (childrenItems!=null) {
							
							node2.setChildrenItems(childrenItems);
							for (ItemOfAssociateToWindow itemOfAssociateToWindow : childrenItems) {
								
								if (ComponentMetaDataConstants.MVC_ITEM_TYPE.equals(itemOfAssociateToWindow.getItemType())) {
									if (itemOfAssociateToWindow.getRegisterEventType()!=null) {
										if (itemOfAssociateToWindow.getGxtToSeamServiceParameter()!=null) {
											if (itemOfAssociateToWindow.getEventStructureMetaModel()!=null) {
												
											} else {
												throw new RuntimeException("sssssssssssssssssssssssssssssssss");

											}
										} else {
											throw new RuntimeException("sssssssssssssssssssssssssssssssss");

										}
									} else {
										throw new RuntimeException("sssssssssssssssssssssssssssssssss");

									}
								}else if (ComponentMetaDataConstants.DATALOAD_ITEM_TYPE.equals(itemOfAssociateToWindow.getItemType())) {
									
								}else if (ComponentMetaDataConstants.COMPONENT_ITEM_TYPE.equals(itemOfAssociateToWindow.getItemType())) {
									
								}else {
									throw new RuntimeException("sssssssssssssssssssssssssssssssss");

								}
							}
						} else {
							throw new RuntimeException("sssssssssssssssssssssssssssssssss");
		
						}
					
				}else if (map2.get(ComponentMetaDataConstants.CHILDREN_ITEMS_TYPE).toString().equals("component")) {
					throw new RuntimeException("sssssssssssssssssssssssssssssssss");
	
				}else {
					throw new RuntimeException("sssssssssssssssssssssssssssssssss");
	
				}
		}else {
			throw new RuntimeException("sssssssssssssssssssssssssssssssss");

		}
		return node2;
		
	}

	public static ItemOfAssociateToWindow createItemForComboBoxOfGridColumn(
			String gridColumnId) {
		Map<String, Object> simpleComboBox01 = new HashMap<String, Object>();			
	    simpleComboBox01.put("component_model_type_index", ComponentTypeEnum.SIMPLECOMBOBOX_MODEL.ordinal()); 		    							
//	    simpleComboBox01.put("id", "positon.positionType");
//	    simpleComboBox01.put("id", "province");
	    simpleComboBox01.put("id", gridColumnId);
	    ItemOfAssociateToWindow item  = new ItemOfAssociateToWindow(simpleComboBox01);
	    item.setItemType(ComponentMetaDataConstants.COMPONENT_ITEM_TYPE);
	    String springComponentName =gridColumnId+"Service";
//	    GxtToSeamServiceParameter gxtToSeamServiceParameter=new GxtToSeamServiceParameter("provinceService", "getAllPersistentEntities" );
	    GxtToSeamServiceParameter gxtToSeamServiceParameter=new GxtToSeamServiceParameter(springComponentName, "getAllPersistentEntities" );
	    gxtToSeamServiceParameter.setMethodArgumentsType(null);
	    item.setGxtToSeamServiceParameter(gxtToSeamServiceParameter);
	    item.setItemType(ComponentMetaDataConstants.COMPONENT_ITEM_TYPE);
	    
	    return item;
	}

	
	
}
